﻿using System;
using System.Collections.Generic;
using System.Linq;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Windsor;
using LIMS.Shared;
using Castle.Core.Logging;
using LIMS.Entities;
using System.Windows.Forms;


namespace LIMS.Windows
{
    public abstract class LIMSPageBase<TPresenter>: Form
    {
        private TPresenter presenter;
        private ILogger ologger;
        /// <summary>
        /// Getter for logger
        /// </summary>
        public ILogger Logger
        {
            get { return ologger; }
        }
        /// <summary>
        /// Getter for presenter
        /// </summary>
        protected TPresenter Presenter
        {
            get { return presenter; }
        }
        /// <summary>
        /// Constructor, initializes page dependencies using Windsor.
        /// </summary>
        protected LIMSPageBase()
        {
            if (this.CreatePresenterAndLogger())
            {
                if (presenter == null)
                    throw new InvalidOperationException("CreatePresenterAndLogger() did not return an instance.");
            }
            else
            {
                    throw new InvalidOperationException("Please check Windsor setting, could not initialize page presenter and log4net.");
            }
        }
        /// <summary>
        /// Resolves presenter and returns it..
        /// </summary>
        /// <returns></returns>
        protected bool CreatePresenterAndLogger()
        {
            try
            {
                IWindsorContainer container = new WindsorContainer(new XmlInterpreter(System.AppDomain.CurrentDomain.BaseDirectory + "\\Components.config"));
                //Resolve logger
                ologger = container.Resolve<ILogger>();
                //Now resolve presenter
                Dictionary<string, object> arguments = new Dictionary<string, object>();
                arguments.Add("view", this);
                presenter = container.Resolve<TPresenter>(arguments);
                return true;
            }
            catch
            {
                return false;  
            }
        }
       
        /// <summary>
        /// Check Page Authorization
        /// </summary>
        /// <param name="p_sSessionId"></param>
        /// <param name="p_sLoggedIn"></param>
        /// <returns></returns>
        public bool CheckPageAuthorization()
        {
            //Removes cache
            //Response.Cache.SetCacheability(HttpCacheability.NoCache);
            ////Check if it is a session and current user owns it. 
            //if (Session[Resource.SESSION_VAR_IS_USER_LOGGED] != null && Session[Resource.SESSION_VAR_SESSION_ID] != null)
            //{
            //    if ((Session[Resource.SESSION_VAR_IS_USER_LOGGED].ToString() == Resource.SESSION_VAL_USER_LOGGED_NO) || (this.Session.SessionID != Session[Resource.SESSION_VAR_SESSION_ID].ToString()))
            //    {
            //        Session[Resource.SESSION_VAR_SESSION_ID] = string.Empty;
            //        return false;
            //    }
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
            return true;
        }
        /// <summary>
        /// Kill Session variables and redirect page to Login page
        /// </summary>
        public void KillSession()
        {
            //Session.Clear();
            //Session.RemoveAll();
            //Session.Abandon();
            ////Redirect user to login page
            //Response.Redirect(Resource.PAGE_LOGIN);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="oControl"></param>
        /// <param name="lstPageData"></param>
        
       
    }
}
